﻿
Imports System.Reflection
Imports std
Imports managedServer
Imports idsysctx
Imports occommon
Imports occontext
Imports avacom

Public Module avatar
    Public Const defaultConfigfile As String = "avatar.ini"
    Public Const cacheSeconds As Int64 = 60 * 5

    Sub New()
        Dim fis() As FieldInfo = Nothing
        fis = GetType(avacom.path._path).GetFields(BindingFlags.Public Or BindingFlags.Static)
        assert(Not fis Is Nothing AndAlso fis.Length() > 0)
        For Each fi As FieldInfo In fis
            assert(strstartwith(Convert.ToString(fi.GetValue(Nothing)), network.pathSeperator))
        Next
    End Sub

    Public Sub main(ByVal args() As String)
        ProHttpS.insertRequestProcessor(New configDelegate())
        ProHttpS.insertRequestProcessor(New intro())
        ProHttpS.insertRequestProcessor(New nick())
        ProHttpS.insertRequestProcessor(New portrait())
        ProHttpS.insertRequestProcessor(New sign())
        ProHttpS.insertRequestProcessor(New idcard())
        ProHttpS.insertRequestProcessor(New homepage())
        ProHttpS.defaultConfigfile = defaultConfigfile
        ProHttpS.startServer(args)
    End Sub

    Private _cache As cacheManager(Of String, map(Of String, String)) = Nothing
    Private _idctx As idlogincontext = Nothing
    Private _ocurl As String = Nothing

    Public Sub initialOcCtx(ByVal ocurl As String)
        copy(_ocurl, ocurl)
    End Sub

    Public Sub initialIdCtx(ByVal idsysUrl As String, ByVal id As String, ByVal pwd As String)
        _idctx = New idlogincontext(id, pwd, Nothing, ocurl())
    End Sub

    Public Sub initialCache(ByVal cachesize As Int64)
        _cache = New cacheManager(Of String, map(Of String, String))(True, cachesize, secondsToTicks(cacheSeconds))
    End Sub

    Private Sub reissueIdCtx()
        _idctx = New idlogincontext(_idctx)
    End Sub

    Private Function cache() As cacheManager(Of String, map(Of String, String))
        assert(Not _cache Is Nothing)
        Return _cache
    End Function

    Private Function idctx() As idlogincontext
        assert(Not _idctx Is Nothing)
        If Not _idctx.loggedIn() Then
            reissueIdCtx()
        End If
        Return _idctx
    End Function

    Private Function ocurl() As String
        assert(Not isemptystring(_ocurl))
        Return _ocurl
    End Function

    Private Function _userSharedCookiesImpl(ByVal id As String, ByRef cookies As map(Of String, String),
                                            ByRef status As Int32, ByRef desc As String) As Boolean
        If idctx().loggedIn() Then
            Dim occtx As ocsitecontext = Nothing
            occtx = New ocsitecontext(idctx().id, idctx().userSiteRandomKey(), id, ocurl())
            Dim rtn As Boolean = False
            rtn = occtx.get()
            If rtn Then
                cookies = occtx.sharedCookies()
            End If
            status = occtx.statusCode()
            desc = occtx.description()
            Return rtn
        Else
            status = response.statusCode.onlineCookieUserSiteRandomKeyError
            desc = response.statusCode.onlineCookieUserSiteRandomKeyError
            Return False
        End If
    End Function

    Private Function _userSharedCookies(ByVal id As String, ByRef cookies As map(Of String, String)) As Boolean
        Dim status As Int32 = 0
        Dim desc As String = Nothing
        If _userSharedCookiesImpl(id, cookies, status, desc) Then
            Return True
        Else
            If status = response.statusCode.onlineCookieUserSiteRandomKeyError Then
                reissueIdCtx()
            End If
            Return _userSharedCookiesImpl(id, cookies, status, desc)
        End If
    End Function

    Public Function userSharedCookies(ByVal id As String, ByRef cookies As map(Of String, String)) As Boolean
        If cache().get(id, cookies) Then
            Return True
        Else
            If _userSharedCookies(id, cookies) Then
                cache().add(id, cookies)
                Return True
            Else
                Return False
            End If
        End If
    End Function

    Public Function userSharedCookie(ByVal id As String, ByVal name As String, ByRef value As String) As Boolean
        Dim c As map(Of String, String) = Nothing
        If userSharedCookies(id, c) Then
            assert(Not c Is Nothing)
            Dim it As map(Of String, String).iterator = Nothing
            it = c.find(name)
            If it <> c.end() Then
                value = (+it).second
                Return True
            Else
                Return False
            End If
        Else
            Return False
        End If
    End Function

    Private Function parseSize(ByVal size As String, ByRef width As Int64, ByRef height As Int64) As Boolean
        If isemptystring(size) Then
            Return False
        Else
            Dim f As String = Nothing
            Dim s As String = Nothing
            Return strseg(size, f, s, avacom.size.separator, False) AndAlso
                   convertor.toint64(f, height) AndAlso convertor.toint64(s, width)
        End If
    End Function

    Public Function parseSize(ByVal context As httpContext, ByRef width As Int64, ByRef height As Int64) As Boolean
        Dim sz As String = Nothing
        sz = context.url().query(avacom.query.size)
        Return parseSize(sz, width, height)
    End Function
End Module
